﻿@using Gurux.DLMS.AMI.Shared.DIs;
@using Gurux.DLMS.AMI.Shared.DTOs
@using System.Globalization
@using Gurux.DLMS.Enums
@using Gurux.DLMS.Objects;

@inject IGXNotifier Notifier
@if (Target != null)
{
    <CascadingValue Value="this">
        <AttributeTile Index="0" Name="@Properties.Resources.Description" Text="@Target.Template.Name"></AttributeTile>
        <AttributeTile Index="1" Name="@Properties.Resources.LogicalName" Text="@Target.Template.LogicalName"></AttributeTile>
        <AttributeTile Index="2" Attributes="@Target.Attributes"></AttributeTile>
        <AttributeTile Index="3" Attributes="@Target.Attributes"></AttributeTile>
        <AttributeTile Index="4" Attributes="@Target.Attributes"></AttributeTile>
        @ChildContent
    </CascadingValue>
}

@code {
    private GXDLMSRegister _register = new();

    [CascadingParameter]
    private ObjectView? Parent { get; set; }

    [Parameter]
    public RenderFragment? ChildContent { get; set; }

    string[]? _states;
    string[]? _modes;

    /// <summary>
    /// Target COSEM object.
    ///</summary>
    [Parameter]
    public GXObject? Target { get; set; }

    void Disconnect()
    {
        Parent?.OnAction(1, (sbyte)0, true);
    }

    void Reconnect()
    {
        Parent?.OnAction(2, (sbyte)0, true);
    }

    /// <summary>
    /// Disconnect control State.
    /// </summary>
    [Parameter]
    public string State
    {
        get
        {
            ObjectView.UpdateValue(Target, _register, 3);
            return _register.Scaler.ToString();
        }
        set
        {
            _register.Scaler = double.Parse(value);
            ObjectView.UpdateValue(Target, _register, 3);
        }
    }

    /// <summary>
    /// Disconnect control mode.
    /// </summary>
    [Parameter]
    public string Mode
    {
        get
        {
            ObjectView.UpdateValue(Target, _register, 4);
            return _register.Unit.ToString();
        }
        set
        {
            _register.Unit = Enum.Parse<Enums.Unit>(value);
            ObjectView.UpdateValue(Target, _register, 4);
        }
    }

    //Add action buttons.
    protected override void OnInitialized()
    {
        try
        {
            if (Parent == null)
            {
                throw new ArgumentNullException(nameof(Parent), "View must exist within a Object view");
            }
            _states = Enum.GetNames(typeof(Gurux.DLMS.Objects.Enums.ControlState));
            _modes = Enum.GetNames(typeof(Gurux.DLMS.Objects.Enums.ControlMode));
            Parent.MenuItems.Add(MenuItem.Create(@Properties.Resources.Disconnect,
                "oi fullscreen-enter",
                Disconnect,
                !Parent.CanInvokeAction(1)));
            Parent.MenuItems.Add(MenuItem.Create(@Properties.Resources.Reconnect,
                "oi fullscreen-exit",
                Reconnect,
                !Parent.CanInvokeAction(2)));
        }
        catch (Exception ex)
        {
            Notifier?.ProcessError(ex);
        }
    }
}